package com.ruoyi.shop.controller;

import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.shop.domain.Orderdetail;
import com.ruoyi.shop.domain.Orders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shop.service.IOrdersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * ordersController
 *
 * @author ruoyi
 * @date 2025-06-08
 */
@Anonymous
@RestController
@RequestMapping("/shop/orders")
public class OrdersController extends BaseController
{
    @Autowired
    private IOrdersService ordersService;

    /**
     * 查询orders列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Orders orders)
    {
        startPage();
        List<Orders> list = ordersService.selectOrdersList(orders);
        return getDataTable(list);
    }
    /**
     * 导出orders列表
     */
    @Log(title = "orders", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Orders orders)
    {
        List<Orders> list = ordersService.selectOrdersList(orders);
        ExcelUtil<Orders> util = new ExcelUtil<Orders>(Orders.class);
        util.exportExcel(response, list, "orders数据");
    }

    /**
     * 获取orders详细信息
     */
    @GetMapping(value = "/{orderid}")
    public AjaxResult getInfo(@PathVariable("orderid") Long orderid)
    {
        return AjaxResult.success(ordersService.selectOrdersByOrderid(orderid));
    }

    /**
     * 新增orders
     */
    @Log(title = "orders", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Orders orders)
    {
        String ordersn= UUID.randomUUID()+"";
        orders.setOrdersn(ordersn);
        List<Orderdetail> list=orders.getOrderdetailList();
        for(Orderdetail od:list){
            od.setOrdersn(ordersn);
        }

        System.out.println(orders.getOrdersn());
        System.out.println(orders.getOrderdetailList().size());
        int i = ordersService.insertOrders(orders);
        if(i > 0){
            return AjaxResult.success("添加成功", ordersn);
        }else {
            return AjaxResult.error("添加失败");
        }
    }

    /**
     * 修改orders
     */
    @Log(title = "orders", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Orders orders)
    {
        return toAjax(ordersService.updateOrders(orders));
    }

    /**
     * 删除orders
     */
    @Log(title = "orders", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderids}")
    public AjaxResult remove(@PathVariable Long[] orderids)
    {
        return toAjax(ordersService.deleteOrdersByOrderids(orderids));
    }
}
